<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>

        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="会员层级">

            <div style="text-align: right; margin-bottom: 20px">
              <el-button type="primary" size="small" @click="clickAdd(1, '0')">添加</el-button>
            </div>
            <el-table row-key="id" :data="list" border>
              <!-- <el-table-column label="id" prop="name"  width="70"> -->
              <el-table-column label="会员表id" prop="id" />
              <el-table-column label="会员名称" prop="name" />
              <el-table-column label="会员折扣" prop="discount" />
              <el-table-column label="消费金额" prop="consume" />
       
              <el-table-column label="操作" fixed="right"  width="80">
                <template slot-scope="scope">
               
                  <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <span>确定删除吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteComfirm">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form v-loading="loading" label-width="90px">
          <el-form-item label="会员名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入会员名称" />
          </el-form-item>
          <el-form-item label="会员折扣" prop="discount">
            <el-input v-model="formData.discount" placeholder="输入你的会员折扣" />
          </el-form-item>
          <el-form-item label="消费金额" prop="consume">
            <el-input v-model="formData.consume" placeholder="请输入" />
          </el-form-item>
      
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确认</el-button>
          </div>
        </template>
      </el-dialog>

    </div>

    <!-- 分页 -->
    <!-- <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="100"
        layout="prev, pager, next, jumper"
        :total="400"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div> -->
  </div>
</template>

<script>

import { selectAllLevel,deleteLevel,insertLevel} from '@/api/role'
export default {
  data() {
    return {
      dialogVisible: false,
      // 分页数据
      currentPage: 1,
      list: [],
      showDialog: false,
      formData: {
      
        name: '', // 名称
        discount: '',
       
      
      },
      loading: false,
      permissionData: [] // 存放用于展示的权限列表数据 (树形结构)
    }
  },
  // computed: {
  //   showTitle() {
  //     if (this.formData.id) {
  //       return '编辑权限'
  //     } else {
  //       return '添加权限'
  //     }
  //   }
  // },
  mounted() {
    console.log('进入页面的钩子函数')
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
   
      const data = await selectAllLevel()
      this.list = data
    },
    clickAdd(type, pid) {
      this.showDialog = true
      // 点击显示弹框时, 立刻记录type, pid
    
    },
    closeDialog() {
      this.showDialog = false
      // 重置清空数据
      this.formData = {
        enVisible: '0', // 0不开启, 1开启 (注意: 不是布尔值, 所以将来需要处理switch)
        name: '', // 名称
        discount:''
      }
    },
    // 点击弹框的确认按钮
    async clickSubmit() {
      if (this.formData.name === '' || this.formData.discount === '') {
        this.$message.error('会员名和折扣不能为空')
        return
      }
      if (this.formData.id) {
        // 编辑操作
        // await reqUpdatePermission(this.formData)
      } else {
        // 添加操作
        await insertLevel(this.formData)
      }
      this.showDialog = false
      this.$message.success('操作成功')
      this.getPermissionList()
    },
    // 删除功能
    handleDelete(id) {
      this.deleteId = id
      this.dialogVisible = true
    },
    async deleteComfirm() {
      this.dialogVisible = false
      await deleteLevel({
        id: this.deleteId
      })
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getPermissionList()
    },
    // 点击查看编辑按钮, 显示弹框
    clickEdit(id) {
      // 显示弹框
      this.showDialog = true
      this.loading = true
      // 发送请求, 获取数据, 进行回显
      // const { data } = await reqGetPermissionDetail(id)
      // this.formData = data

      this.loading = false
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style>

</style>

           